package com.taxsearch.controller.room;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;


import com.taxsearch.dao.room.JingWaiMapper;
import com.taxsearch.dao.room.RoomManagementMapper;
import com.taxsearch.dao.room.RoomMapper;
import com.taxsearch.util.ApiJsonErr;
import com.taxsearch.util.ApiJsonRet;
import com.taxsearch.util.roomutil.RoomUtil;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;





/**
 * @description: 房间增删改操作
 *
 * @author: 杜一平
 *
 * @create: 2018年9月11日-上午10:34:10
 **/
@Controller
@RequestMapping("/HotelAndroid")
public class RoomAddDeleteUpdateController {
	/**
	 * 总体说明：
	 * 参数data为一个json字符串，类似为url地址后面的localhost:8080/HotelAndroid/selectRoom?data
	 * ={"status":"1"}中的data 注意调用相关方法解析
	 * 返回值为Object,其中返回ApiJsonErr默认状态为400为失败,ApiJsonRet状态为200为成功
	 */

	// 日志
	public static final Logger logger = LogManager
			.getLogger(RoomManageController.class);
	
	// 统一返回结果
	Object result;

	// 房间管理以及其他的dao层
	@Autowired
	private RoomManagementMapper roommanagementmapper;
	
	// 境外相关操作
	@Autowired
	private JingWaiMapper jingWaiMapper;
	
	@Autowired
	private RoomMapper roomMapper;


	/**
	 * 
	 * @Title: updateRoom
	 * @Description: 更新房间
	 * @param: @param data
	 * @param: @return
	 * @return: Object
	 * @throws
	 */
	@RequestMapping("/updateRoom.do")
	@ResponseBody
	public Object updateRoom(String data) {
		try {
			// 获取相关参数
			Map<String, String> map = RoomUtil.getJsonMap(data);
			
			// 校验
//			if (RoomUtil.validationData(map)) {
//				return new ApiJsonErr("有参数为空！");
//			}
			
			// 获取刘佳鑫可以单个修改房间号
			String type=map.get("type");
			
			// 取出房间尾数
			String removeNum = map.get("removeNum");
			// 房间开始数字
			String startNum = map.get("startNum");
			// 房间结束数字
			String endNum = map.get("endNum");
			// 旅馆id
			String hotelId = map.get("hotelId");


			int startInt = Integer.parseInt(startNum);
			int endInt = Integer.parseInt(endNum);
			String[] removeStrings = removeNum.split(",");

			// 去掉需要被去除的尾数
			List<String> list = new LinkedList<>();
			RoomUtil.getCaoZuoList(list, removeStrings, startInt, endInt);
			// 如果被操作的房间没有
			if (list.size() == 0) {
				return new ApiJsonErr("请添加需要操作的房间！");
			}
			
			//当状态为0时判断修改的房间号是否已经存在
			if("0".equals(type)){
				List<String>newRoomList=new ArrayList<>();
				newRoomList.add(map.get("new_num"));
				
				List<String>judgeUpdateRoom=roomMapper.judgeInsertRoom(newRoomList, hotelId);
				if(judgeUpdateRoom.size()>=1){
					return new ApiJsonErr("修改的房间已存在");
				}
			}
			
			// 判断修改的房间号是否存在
			List<String>caozuoList=roomMapper.judgeInsertRoom(list, hotelId);
			if(caozuoList.size()==0){
				return new ApiJsonErr("请输入正确的房间！");
			}

			// 查询所有入住信息
			List<Map<String, String>> ruzhuNumList = roommanagementmapper
					.selectRuZhuXX(hotelId, null);
			// 查询境外入住
			List<Map<String, String>>selectJingWaiList=jingWaiMapper.selectJingWai(hotelId, null);
			ruzhuNumList.addAll(selectJingWaiList);
			
			// 判断是否有入住房间
			List<String> ruzhuList = new LinkedList<>();
			RoomUtil.judgeRuzhu(ruzhuList,ruzhuNumList, list, hotelId);
			if (ruzhuList.size() != 0) {
				return new ApiJsonErr("以下房间为入住状态:" + ruzhuList.toString()
						+ " ！");
			}
			// 判断房间是否被预定
			List<String> yuDingList = roommanagementmapper
					.selectYuDingXXByNumberRoom(list, hotelId,
							RoomUtil.getEightDateNow());
			if (yuDingList.size() != 0) {
				return new ApiJsonErr("以下房间为预定状态:" + yuDingList.toString()
						+ " ！");
			}

			int num=0;
			// 0为单个可以修改房间号，1为批量
			// 如果可能修改房间号
			if("0".equals(type)){
				// 修改房间
				num=roomMapper.updateRoom(map);
				// 修改房间金额
				num=roomMapper.updateRoomJe(map);
			}else{
				// 执行批量操作
				// max不为空才执行更新
				if(map.containsKey("max")&&!"".equals(map.get("max"))){

					// 最大入住人数
					String max = map.get("max");
					num=roomMapper.updateRoomByBatch(list,hotelId,max);
				}
				// 第二个mybatis操作是采用批量实行sql，第一个是in 循环listq
				// 假设没有金额就不做金额修改
				if(map.containsKey("price")&&!"".equals(map.get("price"))){
					// 房间价格
					String price = map.get("price");			
					int num2=roomMapper.updateRoomJeByBatch(list,hotelId,price);
				}
			}
			System.out.println("返回成功");
			result = new ApiJsonRet("200", "修改成功！");
		} catch (Exception e) {
			logger.error("updateRoom-发生错误!!!!!!!!!!!!!!!!!!!!!!!!", e);
			result = new ApiJsonErr("服务器繁忙,请稍后再试！");
		}
		return result;
	}

	/**
	 * 
	 * @Title: insertRoom
	 * @Description: 添加房间
	 * @param: @param data
	 * @param: @return
	 * @return: Object
	 * @throws
	 */
	@RequestMapping("/insertRoom.do")
	@ResponseBody
	public Object insertRoom(String data) {
		try {
			// 获取相关参数
			Map<String, String> map = RoomUtil.getJsonMap(data);
			// 校验
			if (RoomUtil.validationData(map)) {
				return new ApiJsonErr("有参数为空！");
			}
			String removeNum = map.get("removeNum");
			String startNum = map.get("startNum");
			String endNum = map.get("endNum");
			String max = map.get("max");
			String price = map.get("price");
			String hotelId = map.get("hotelId");
			int startInt = Integer.parseInt(startNum);
			int endInt = Integer.parseInt(endNum);
			String[] removeStrings = removeNum.split(",");

			// 去掉需要被去除的尾数
			List<String> list = new LinkedList<>();
			RoomUtil.getCaoZuoList(list, removeStrings, startInt, endInt);
			// 如果被操作的房间没有
			if (list.size() == 0) {
				return new ApiJsonErr("请添加需要操作的房间！");
			}

			// 判断是否有入住房间
			List<String> yiyouList = roomMapper.judgeInsertRoom(list,
					hotelId);
			if (yiyouList.size() != 0) {
				return new ApiJsonErr("已有房间号:" + yiyouList.toString() + " ！");
			}

			// 获取批量执行list
			List<Map<String, String>> insertList = new LinkedList<>();
			RoomUtil.goBatch(insertList, hotelId, max, price, list);

			// 执行批量操作
			roomMapper.insertRoomByBatch(insertList);
			roomMapper.insertRoomJeByBatch(insertList);
			
			System.out.println("返回成功");
			result = new ApiJsonRet("200", "添加成功！");
		} catch (Exception e) {
			logger.error("insertRoom-发生错误!!!!!!!!!!!!!!!!!!!!!!!!", e);
			result = new ApiJsonErr("服务器繁忙,请稍后再试！");
		}
		return result;

	}
	
	/**
	 * 
	 * @Title: deleteRoom
	 * @Description: 删除房间
	 * @param: @param data
	 * @param: @return
	 * @return: Object
	 * @throws
	 */
	@RequestMapping("/deleteRoom.do")
	@ResponseBody
	public Object deleteRoom(String data) {
		try {
			// 获取相关参数
			Map<String, String> map = RoomUtil.getJsonMap(data);
			// 校验
			if (RoomUtil.validationData(map)) {
				return new ApiJsonErr("有参数为空！");
			}

			String removeNum = map.get("removeNum");
			String startNum = map.get("startNum");
			String endNum = map.get("endNum");
			String hotelId = map.get("hotelId");
			int startInt = Integer.parseInt(startNum);
			int endInt = Integer.parseInt(endNum);
			String[] removeStrings = removeNum.split(",");

			// 去掉需要被去除的尾数
			List<String> list = new LinkedList<>();
			RoomUtil.getCaoZuoList(list, removeStrings, startInt, endInt);
			// 如果被操作的房间没有
			if (list.size() == 0) {
				return new ApiJsonErr("请添加需要操作的房间！");
			}

			// 查询所有入住信息
			List<Map<String, String>> ruzhuNumList = roommanagementmapper
					.selectRuZhuXX(hotelId, null);
			
			// 查询境外入住
			List<Map<String, String>>selectJingWaiList=jingWaiMapper.selectJingWai(hotelId, null);
			ruzhuNumList.addAll(selectJingWaiList);
			
			// 判断是否有入住房间
			List<String> ruzhuList = new LinkedList<>();
			RoomUtil.judgeRuzhu(ruzhuList,ruzhuNumList, list, hotelId);
			if (ruzhuList.size() != 0) {
				return new ApiJsonErr("以下房间为入住状态:" + ruzhuList.toString()
						+ " ！");
			}
			// 判断房间是否被预定
			List<String> yuDingList = roommanagementmapper
					.selectYuDingXXByNumberRoom(list, hotelId,
							RoomUtil.getEightDateNow());
			if (yuDingList.size() != 0) {
				return new ApiJsonErr("以下房间为预定状态:" + yuDingList.toString()
						+ " ！");
			}

			// 执行批量操作
			int num=roomMapper.deleteRoom(list, hotelId);
			int num2=roomMapper.deleteRoomJe(list, hotelId);
			if(num<1){
				result = new ApiJsonErr("不存在该房间！");
			}else{
				result = new ApiJsonRet("200", "删除成功！");
			}
		} catch (Exception e) {
			logger.error("deleteRoom-发生错误!!!!!!!!!!!!!!!!!!!!!!!!", e);
			result = new ApiJsonErr("服务器繁忙,请稍后再试！");
		}
		System.out.println("返回成功");
		return result;

	}
}
